package com.tttshaoqi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tttshaoqi.entity.SysUser;
import com.tttshaoqi.entity.SysUserAuth;
import com.tttshaoqi.service.SysUserService;
import com.tttshaoqi.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.switchuser.SwitchUserGrantedAuthority;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author tttshaoqi
* @description 针对表【sys_user(用户表)】的数据库操作Service实现
* @createDate 2025-07-30 17:38:05
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService, UserDetailsService {

    @Autowired
    private SysUserMapper sysUserMapper;

    // 查询数据库，进行登录校验
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
         SysUser user = sysUserMapper.getByUsername(username);
         if(user==null){
             return null;
         }
         SysUserAuth sysUserAuth = new SysUserAuth();
         sysUserAuth.setUsername(user.getUsername());
         sysUserAuth.setPassword(user.getPassword());
         sysUserAuth.setNickname(user.getNickName());
         // 添加角色
         List<String> roles = new ArrayList<>();
         if(username.equals("user@qq.com")){
             roles.add("USER");
         }else if(username.equals("admin@qq.com")){
             roles.add("ADMIN");
             roles.add("USER");
         }
         sysUserAuth.setRoles(roles);
         return sysUserAuth;
    }
}




